package com.primaryedu.ui.app.box;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.primaryedu.R;
import com.primaryedu.data.Contants;
import com.primaryedu.data.GlobalData;
import com.primaryedu.ui.BaseActivity;
import com.primaryedu.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 信箱填写
 *
 * @author jun.wang
 * @description
 * @data 2018年1月8日
 */
public class BoxWriteActivity extends BaseActivity implements OnClickListener {

    private EditText et_boxwrite;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_box_write);

        init();

    }

    private void init() {

        ((TextView) findViewById(R.id.title)).setText("校长信箱");
        findViewById(R.id.back).setOnClickListener(this);
        findViewById(R.id.bt_boxwrite_submit).setOnClickListener(this);
        //内容
        et_boxwrite = (EditText) findViewById(R.id.et_boxwrite);

    }

    @Override
    protected int BindLayout() {
        return R.layout.activity_box_write;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.back:
                finish();
                break;

            case R.id.bt_boxwrite_submit:
                if (Utils.isBlank(et_boxwrite.getText().toString())) {
                    showhintDialog("提示", "请输入您的宝贵意见！");
                } else {
                    requestMasterBoxlist();
                }
//			 showToast("提交成功！");
//			 finish();
                break;

            default:
                break;
        }

    }

    private void requestMasterBoxlist() {
        showLoading("提交中...");
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Contants.masterboxsaveList,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        dismissLoading();
                        System.out.println("response=masterboxlist=======" + response);

                        try {
                            JSONObject json = new JSONObject(response);
                            int result = json.getInt("result");

                            if (result == 1) {
                                showToast("提交成功！");
                                finish();
                            } else {
                                showhintDialog("提示失败", "请检查是否有特殊字符！");
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                errorLoading("提交失败，请重新提交！");
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> map = new HashMap<String, String>();
                map.put("userId", GlobalData.getUserId(getApplicationContext()));
                map.put("userName", GlobalData.getUsername(getApplicationContext()));
                map.put("content", et_boxwrite.getText().toString());
                return map;

            }
        };
        requestQueue.add(stringRequest);
    }

}
